package cn.tede.file;

import java.io.*;

public class TestCopy {
    public static void main(String[] args) {
        copy("G:\\IO\\2.txt","G:\\IO\\test.txt");
    }
    /**
     * 提供字节流的复制操作
     * @param start 第一个参数为文件初始位置，
     * @param end 第二个参数为文件要复制的位置
     */
    public static void copy(String start,String end) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream=new BufferedInputStream(new FileInputStream(start));
         //   outputStream=new BufferedOutputStream(new FileOutputStream(end));//此方法会覆盖掉原有文件
            outputStream=new BufferedOutputStream(new FileOutputStream(end,true));
            byte a[]=new byte[10000];
            int b=0;
            while ((b=inputStream.read(a,0, a.length))!=-1){
                for (int i = 0; i < b; i++) {
                    outputStream.write(a[i]);
                }
            }
            outputStream.flush();

            System.out.println("文件复制成功OVER");
        } catch (IOException e) {
            System.out.println("文件路径异常");
        }finally {
            //inputStream流关闭操作
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    System.out.println("关闭异常");
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
